<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 李邦琦 <1536549251@qq.com>
// +----------------------------------------------------------------------

namespace app\crm\model;
use think\Db;
use think\Model;
use think\Cache;
use think\Loader;

class Member extends Model
{
    /**
     * 缓存信息
     */
    protected function membercache ()
    {
        $result = Db::name('vip_membership_level')->order('groups')->select();
        foreach ($result as &$v) {
            switch ($v['groups']) {
                case '1': $v['member_level'] = 'a类客户'; break;
                case '2': $v['member_level'] = 'b类客户'; break;
                case '3': $v['member_level'] = 'c类客户'; break;
                case '4': $v['member_level'] = 'd类客户'; break;
                case '5': $v['member_level'] = 'e类客户'; break;
                default: $v['member_level'] = '不成单'; break;
            }
            $data[$v['groups']] = $v;
        }
        Cache::set('member', $data);
    }


    /**
     * 获取病情类型列表
     * @return array <日报列表信息>
     */
    public function memberLevelData()
    {
        $page = $_REQUEST["page"]; // 页码
        $limit = $_REQUEST["limit"]; // 每页显示条数
        $count = Db::name('vip_membership_level')->count();
        // 查询状态为1的用户数据 并且每页显示10条数据
        $memberLevelDataList = Db::name('vip_membership_level')->limit(($page-1)*$limit,$limit)->order('id ASC')->select();
        foreach ($memberLevelDataList as &$val) {
            switch ($val['groups']) {
                case '1': $val['groups'] = 'a类客户'; break;
                case '2': $val['groups'] = 'b类客户'; break;
                case '3': $val['groups'] = 'c类客户'; break;
                case '4': $val['groups'] = 'd类客户'; break;
                case '5': $val['groups'] = 'e类客户'; break;
                case '6': $val['groups'] = '不成单'; break;
            }
        }
        $rowsList = count($memberLevelDataList) ? $memberLevelDataList : false;
        $arrData["code"] = 0;
        $arrData["msg"] = '';
        $arrData["count"] = $count;
        $arrData["data"] = $rowsList;
        return $arrData;
    }


    /**
     * 删除病情类型
     * @param  string $id 病情类型id
     * @return json       删除状态
     */
    public function delMemberLevel ($id)
    {
        $result = Db::name('vip_membership_level')->delete($id);
        if ($result) {
            $this->membercache();
            echo json_encode(array('success'=>true,'msg'=>'删除成功！'));
        } else {
            echo json_encode(array('msg'=>'删除失败！'));
        }
    }


    /**
     * 新增病情类型
     * @param array  $data  数据
     * @return json         新增状态
     */
    public function insertMemberLevel ($data)
    {
        $result = Db::name('vip_membership_level')->insert($data);
        if ($result) {
            $this->membercache();
            echo json_encode(array('success'=>true,'msg'=>'添加成功！'));
        } else {
            echo json_encode(array('msg'=>'添加失败！'));
        }
    }


    /**
     * 编辑病情类型
     * @param array  $data  数据
     * @return json         新增状态
     */
    public function updateMemberLevel ($data)
    {
        $result = Db::name('vip_membership_level')->update($data);
        if ($result) {
            $this->membercache();
            echo json_encode(array('success'=>true,'msg'=>'编辑成功！'));
        } else {
            echo json_encode(array('msg'=>'编辑失败！'));
        }
    }


    /**
     * 获取单条病情类型信息
     * @param string $id    数据id
     * @return array        选项信息
     */
    public function getMemberLevel ($id)
    {
        $result = Db::name('vip_membership_level')->where("id=$id")->find();
        return $result;
    }


}